package com.zhouyibike.uu.ui.activity.mywallet;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.LoginResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;
import com.zhouyibike.uu.utils.Base64Utils;
import com.zhouyibike.uu.utils.RSAUtils;
import com.zhouyibike.uu.utils.Util;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

public class ShoukuanActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private ImageView shoukuan_erweimaiv;
    /* 密钥内容 base64 code */
    private static String PUCLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrINgjK0/MQLHrFCS9E+VjVWAS L6RG8mY+PuvuUGuaRCGNupp39qHgBC1Bzf+eSGLDrWSiYti8b1cvIgvJWiCFDGaN wzVMs/tSL8QIK1jYLt/vMr4c8O+mmxWmU9hqHLBZnpnslf8ik7AR9ppkrYHuPBxl PH0y2e7utbnAYvkoZwIDAQAB";
    private static String PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKsg2CMrT8xAsesU JL0T5WNVYBIvpEbyZj4+6+5Qa5pEIY26mnf2oeAELUHN/55IYsOtZKJi2LxvVy8i C8laIIUMZo3DNUyz+1IvxAgrWNgu3+8yvhzw76abFaZT2GocsFmemeyV/yKTsBH2 mmStge48HGU8fTLZ7u61ucBi+ShnAgMBAAECgYEAmVdL9W+pCIhsJQ6ewQKkCLeG 56TjGmO4tEwjkx9i3A/y1RIBuj843AhCGXYieE26mka0J0JVnbxTLjveveVRvw78 m1VoMuP9zddV4xC4q2kL+V683i1BTximkre8yqAVMfQr1EE6LNIlfU8XbyEHCv1d 7DhneRZEu+u7sH6SrRkCQQDe7HPlQGIp8PFfK7+MAaKszOonIKlgUeHsiZvPbpR3 gwWbz25iEoRFevqlEBx+/Z7/+6zL5lVm0dlvm//r2bkzAkEAxIT+WMBl+xHhmv1d jHziyYfBcUyQoLnkmc6SNlTs/oNVKh4AlKERLi/cv67zkevWp6Dqj8bv8WUHN6vT KChb/QJAOywgh1XTuLZQG6cNLSix+ZRbAw/UZdDWW2cbiLAdkKuirX0eG8I2ggrm xTHFnp/LuL8hWu5E9Qn24X1TLVOZqQJAJiOldTYJ1Ls4rKrFcBGAthoJueJhI0BQ lGUZzT+YKyDz81OVS0MjIXKahfz/Om+p3iiFvVefk5rG1tAbD+0dRQJATKPCyHwY HWyqrEGrVsfigY6TuRDG/3s22ikjv4UQZ5227i+TwcJT3e9d3yRNm3rPm/TjK37s LSgxtsbrfSuGwg==";
    private String gongyao = "";
    private String source="";
    private TextView shouquanjilu;
    //    QRCodeForPayment
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shoukuan);
        initView();
        source="zy"+"@"+preferences.getString("mobile","zy")+"@"+preferences.getString("fullName","zy")+"@"+preferences.getString("headPicture","zy");
        try {
            PublicKey publicKey = RSAUtils.loadPublicKey(PUCLIC_KEY);
            PrivateKey privateKey = RSAUtils.loadPrivateKey(PRIVATE_KEY);
            byte[] encryptByte = RSAUtils.encryptByPublicKey(source.getBytes(), publicKey);
            String afterencrypt = Base64Utils.encode(encryptByte);
            Log.e("onCreate1: ", afterencrypt);
            createQRImage(afterencrypt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        shouquanjilu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i=new Intent(ShoukuanActivity.this,MyDetailActivity.class);
                i.putExtra("type","30");
                startActivity(i);
            }
        });
    }

    private void initView() {
        QR_WIDTH = Util.dp2px(this, 200);
        QR_HEIGHT = Util.dp2px(this, 200);
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("二维码收券");
        mBtnBack.setOnClickListener(this);
        shoukuan_erweimaiv = (ImageView) findViewById(R.id.shoukuan_erweimaiv);
        shouquanjilu= (TextView) findViewById(R.id.shouquanjilu);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_txsuccess_detail:
                Intent intent = new Intent(ShoukuanActivity.this, MyDetailActivity.class);
                intent.putExtra("type", "13");
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    private int QR_WIDTH;
    private int QR_HEIGHT;

    //要转换的地址或字符串,可以是中文
    public void createQRImage(String url) {
        try {
            //判断URL合法性
            if (url == null || "".equals(url) || url.length() < 1) {
                return;
            }
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            //图像数据转换，使用了矩阵转换
            BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            //下面这里按照二维码的算法，逐个生成二维码的图片，
            //两个for循环是图片横列扫描的结果
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }
            //生成二维码图片的格式，使用ARGB_8888
            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            //显示到一个ImageView上面
            shoukuan_erweimaiv.setImageBitmap(bitmap);
            shoukuan_erweimaiv.setVisibility(View.VISIBLE);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }
//
    /**
     * 获取公钥
     */
    private void getMsg() {
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token", ""));
        showProgressDialog();
        addSubscription(apiStores.ReloadAccountInfo(params),
                new ApiCallback<LoginResult>() {

                    @Override
                    public void onSuccess(LoginResult model) {
                        if (model.getStatus() == Config.SUCCESSCODE) {

                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();

                    }
                });
    }

    @Override
    protected void onPause() {
        setResult(321);
        super.onPause();
    }
}
